﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WebCamCapture
{
    public partial class ChooseCamera : Form
    {
        public static CaptureDevice SelectCamera()
        {
            List<CaptureDevice> devices = CaptureDevice.GetDevices();
            if (devices.Count == 0)
            {
                MessageBox.Show("No devices found");
                return null;
            }
            else if (devices.Count == 1)
            {
                return devices[0];
            }
            else
            {
                using (ChooseCamera dlg = new ChooseCamera(devices))
                {
                    dlg.ShowDialog();
                    return dlg._SelectedDevice;
                }
            }
        }

        CaptureDevice _SelectedDevice;

        private ChooseCamera(List<CaptureDevice> devices) : this()
        {
            foreach (CaptureDevice device in CaptureDevice.GetDevices())
            {
                cboDevices.Items.Add(device);
            }

            cboDevices.SelectedIndex = 0;
        }
        public ChooseCamera()
        {
            InitializeComponent();
        }



        private void startButton_Click(object sender, EventArgs e)
        {
            _SelectedDevice = cboDevices.SelectedItem as CaptureDevice;
            Close();
        }
    }
}
